package com.ywl.study.sharding.study06.factory;

import com.ywl.study.sharding.common.enums.ShardingType;
import com.ywl.study.sharding.study06.config.precise.MasterSlaveConfiguration;
import com.ywl.study.sharding.study06.config.range.ShardingDatabasesAndTablesConfigurationRange;
import com.ywl.study.sharding.study06.config.range.ShardingDatabasesConfigurationRange;
import com.ywl.study.sharding.study06.config.range.ShardingMasterSlaveConfigurationRange;
import com.ywl.study.sharding.study06.config.range.ShardingTablesConfigurationRange;

import javax.sql.DataSource;
import java.sql.SQLException;

public class RangeDataSourceFactory {
    public static DataSource newInstance(final ShardingType shardingType) throws SQLException {
        switch (shardingType) {
            case SHARDING_DATABASES:
                return new ShardingDatabasesConfigurationRange().getDataSource();
            case SHARDING_TABLES:
                return new ShardingTablesConfigurationRange().getDataSource();
            case SHARDING_DATABASES_AND_TABLES:
                return new ShardingDatabasesAndTablesConfigurationRange().getDataSource();
            case MASTER_SLAVE:
                return new MasterSlaveConfiguration().getDataSource();
            case SHARDING_MASTER_SLAVE:
                return new ShardingMasterSlaveConfigurationRange().getDataSource();
            default:
                throw new UnsupportedOperationException(shardingType.name());
        }
    }
}
